﻿namespace KingerRobot.MexicoSailunMDC.Biz;
/// <summary>
/// 异常口下胎
/// </summary>
public class AbnormalOutLocBiz : K8.MDCBB.Biz
{
    #region 配置参数
    private ParamData Param { get; set; }

    class ParamData
    {

        public long LocId { get; set; }
   
    }
    #endregion

    #region PLC 通讯参数
    private ValueData Data { get; set; }
    class ValueData
    {

        ///// <summary>
        ///// 产品条码
        ///// </summary>
        //public string ProductCode { get; set; }
        //下面是读取信息
        //任务号
        public int TaskNo { get; set; }
    

    }
    #endregion

    #region 缓存
    private CacheData Cache { get; set; }
    class CacheData
    {
        //任务号
        public int? TaskNo { get; set; }



        public DateTime catchDate { get; set; }

        public long locId { get; set; }
    }
    #endregion

    #region 实时日志
    private StepData Step { get; set; }
    class StepData
    {
        private string _msg = string.Empty;
        public string StepMsg
        {
            get
            {
                return this._msg;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    this._msg = string.Empty;
                    return;
                }

                this._msg = $"{System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}>>{value}";
            }
        }
    }
    private void StepMsg(string msg)
    {
        this.Step.StepMsg = msg;
        this.WriteLog(msg);
    }
    #endregion

    #region 文件日志（运行需要 Nlog 配置）
    private void WriteLog(string message)
    {
        K8.LSF.Client.Log(new K8.LSF.Log { Data = message });
    }
    #endregion

    /// <summary>
    /// 和 PLC 通讯对象
    /// </summary>
    private BizData BizData { get; set; }
    public int Execute(BizData writer)
    {
        // Excute 方法外使用 BizParam
        this.BizData = writer;

        // 设备故障
        string errorCode = string.Empty;
        StringBuilder stringBuilder = new StringBuilder();
        this.StepMsg($"开始执行异常口完成");

        // 返回参数
        var result = 0;
        var taskNo = Data.TaskNo;
        this.StepMsg($"任务号{taskNo}开始执行结束00");
        var service = K8.RSF.Client.Create<ITaskService>();

        if (taskNo == 0)
        {
            this.StepMsg($"任务号{taskNo}不存在");
            return result;
        }



        if (taskNo == Cache.TaskNo)
        {
            this.StepMsg($"任务号 {taskNo}已完成异常口下胎操作");
            return result;
        }
        try
        {
            //结束任务并推送完成
            var num = service.FinishAbnormOutTask(taskNo);
            //if (num > 0)
            //{
                this.StepMsg($"任务号 {taskNo}完成异常口下胎操作");
                Cache.TaskNo = taskNo;
                return result;
            //}
        }
        catch (Exception ex)
        {
            this.StepMsg($"任务号{taskNo}开始执行结束异常{ex.ToString()}");
        }
        return result;
    }

}
